/**
 *  Copyright (c) 2006, Reliable Source, Inc. All Rights Reserved
 */
package org.reliablesource.prism.core.tests;

import junit.framework.TestCase;

import org.reliablesource.prism.core.IPrismElementFactory;
import org.reliablesource.prism.core.IPrismScreen;
import org.reliablesource.prism.core.IPrismTutorial;
import org.reliablesource.prism.core.IPrismUnit;
import org.reliablesource.prism.core.plugin.PrismCorePlugin;

/**
 *
 *  PrismTutorialTest.java
 *
 *	Created on: Sep 20, 2006 
 *  @author Dennis Park 
 *  	<a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 */
public class PrismTutorialTest extends TestCase {
	
	IPrismElementFactory factory;
	IPrismTutorial tutorial;
	/**
	 * @param arg0
	 */
	public PrismTutorialTest(String arg0) {
		super(arg0);
		factory = PrismCorePlugin.getDefault().getElementFactory();
		assertNotNull(factory);
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
		assertNull(tutorial);
		tutorial = factory.createTutorialNode();
		assertNotNull(tutorial);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
		assertNotNull(tutorial);
		tutorial = null;
		assertNull(tutorial);
	}

	/**
	 * Test method for {@link org.reliablesource.prism.core.internal.PrismTutorial#PrismTutorial()}.
	 */
	public final void testPrismTutorial() {
		assertEquals("Model contains more than one tutorial.", tutorial, factory.createTutorialNode());
		assertNotNull(tutorial.getId());
		assertNotNull(tutorial.getName());
		assertNotNull(tutorial.getParent());
	}

	/**
	 * Test method for {@link org.reliablesource.prism.core.internal.PrismNode#PrismNode()}.
	 */
	public final void testPrismNode() {		
		assertNotNull(tutorial.getId());
		assertNotNull(tutorial.getName());
		assertNotNull(tutorial.getParent());
	}

	/**
	 * Test method for {@link org.reliablesource.prism.core.internal.PrismNode#getChildren()}.
	 */
	public final void testGetChildren() {
		assertNotNull("Tutorial returned null on getChildren", tutorial.getChildren());
		int nChildren = tutorial.getChildren().length;		
		factory.createScreen();
		int n1Children = tutorial.getChildren().length;
		assertTrue("Tutorial getChildren failed after child creation",nChildren+1 == n1Children);
	}

	/**
	 * Test method for {@link org.reliablesource.prism.core.internal.PrismNode#addChild(org.reliablesource.prism.core.IPrismUnit)}.
	 */
	public final void testAddChild() {
		int preCreate = tutorial.getChildListSize();
		factory.createScreen();
		assertTrue("Post-screen creation, tutorial has no children.", tutorial.getChildListSize() > preCreate);
		factory.createScreen();
		factory.createScreen();
		assertTrue("Post-screen creation 2 , tutorial does not have 3 children.",
				tutorial.getChildListSize() == preCreate + 3);	
	}

	/**
	 * Test method for {@link org.reliablesource.prism.core.internal.PrismNode#removeChild(org.reliablesource.prism.core.IPrismUnit)}.
	 */
	public final void testRemoveChild() {
		int preCreate = tutorial.getChildListSize();
		IPrismScreen scr1 = factory.createScreen();
		assertTrue("Post-screen creation, tutorial has no children.", tutorial.getChildListSize() > preCreate);
		IPrismScreen scr2 = factory.createScreen();
		IPrismScreen scr3 = factory.createScreen();
		assertTrue("Post-screen creation 2 , tutorial children is not > preCreate children length.",
				tutorial.getChildListSize() > preCreate);
		tutorial.removeChild(scr1);
		tutorial.removeChild(scr2);
		tutorial.removeChild(scr3);
		
		assertTrue("num. children is not equal to preCreate, after symmetric removal.", 
				tutorial.getChildListSize() == preCreate);
	}

	/**
	 * Test method for {@link org.reliablesource.prism.core.internal.PrismNode#getChild(int)}.
	 */
	public final void testGetChildInt() {
		
		int nChildren = tutorial.getChildren().length;
		Object [] children = tutorial.getChildren();
		
		for(int i = 0; i < nChildren; i++)
			assertSame("getChildInt call failed.", children[i], tutorial.getChild(i));
	}

	/**
	 * Test method for {@link org.reliablesource.prism.core.internal.PrismNode#containsChild(org.reliablesource.prism.core.IPrismUnit)}.
	 */
	public final void testContainsChildIPrismUnit() {
		int nChildren = tutorial.getChildren().length;
		Object [] children = tutorial.getChildren();
		
		for(int i = 0; i < nChildren; i++)
			assertTrue("tutorial failed to verify childs' membership.",tutorial.containsChild((IPrismUnit) children[i]));
	}

	/**
	 * Test method for {@link org.reliablesource.prism.core.internal.PrismNode#getChildSequence(org.reliablesource.prism.core.IPrismUnit)}.
	 */
	public final void testGetChildSequence() {
		int nChildren = tutorial.getChildren().length;
		Object [] children = tutorial.getChildren();
		
		for(int i = 0; i < nChildren; i++)
			assertSame("tutorial failed to retrieve child by sequence.",tutorial.getChild(i), children[i]);
	}

}
